<?php
namespace app\admin\plug;

use app\admin\library\User;
use cubox\Cubox;

class AdminPowerExtendCheck{
    public function searchAdminUser($power,$data){
        if (User::super()) return true;
        if (User::getPowerExtend('admin',$power,'searchAdminUser') == 'all') return true;
        if ($data == 'self' || $data=='custom') return true;
    }

    public function searchUserGroupList($power,$data){
        if (User::super()) return true;

        $searchAdminUser =  User::getPowerExtend('admin',$power,'searchAdminUser','self');

        if ($searchAdminUser== 'all') return true;
        
        $myGroupList = User::get('admin-search-grouplist');
        if (!$myGroupList ) {
            $myGroup = [];

            if ($searchAdminUser == 'self'){
                $myGroup[] = ['group_id'=>User::get('group_id')];
            }

            if ($searchAdminUser == 'custom'){
                $myGroup[] = User::getPowerExtend('admin',$power,'searchUserGroupList',[]);
            }
            $myGroupList = $this->getMyGroupList( $myGroup);
            User::set('admin-search-grouplist',$myGroupList);
        }

        foreach($data as $item){
            if (!in_array($item['group_id'],$myGroupList)){
                return false;
            }
        }

        return true;
    }


    public function manageAdminUserGroup($power,$data){
        if (User::super()) return true;
        if (User::getPowerExtend('admin',$power,'manageAdminUserGroup') == 'all') return true;
        if ($data == 'self' || $data=='custom') return true;
    }
    
    public function manageGroupList($power,$data){
        if (User::super()) return true;

        $searchAdminUser =  User::getPowerExtend('admin',$power,'manageAdminUserGroup','self');

        if ($searchAdminUser== 'all') return true;
        
        $myGroupList = User::get('admin-manage-grouplist');
        if (!$myGroupList ) {
            $myGroup = [];

            if ($searchAdminUser == 'self'){
                $myGroup[] = ['group_id'=>User::get('group_id')];
            }

            if ($searchAdminUser == 'custom'){
                $myGroup[] = User::getPowerExtend('admin',$power,'manageGroupList',[]);
            }
            $myGroupList = $this->getMyGroupList( $myGroup);
            User::set('admin-manage-grouplist',$myGroupList);
        }

        foreach($data as $item){
            if (!in_array($item['group_id'],$myGroupList)){
                return false;
            }
        }

        return true;
    }

    private function getMyGroupList(array $groupList):array{

        $list = [];
        if (count($groupList)>0){
            $app = Cubox::app('admin');
            $group = $app->model('UserGroup');
 
            foreach($groupList as $groupItem){
                if (!in_array($groupItem['group_id'],$list)){
                    $list = array_merge($list,$group->getKeyCollect($groupItem['group_id']));
                }
            }
        }

        return $list;
    }


}